package com.company.managesystem.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.company.managesystem.R;
import com.company.managesystem.common.Apis;
import com.company.managesystem.common.DataFetch;
import com.company.managesystem.common.MyTextUtils;
import com.company.managesystem.model.LoginBean;
import com.company.managesystem.model.LoginItem;
import com.company.managesystem.util.SharePreferenceUtil;
import com.company.managesystem.util.ToastUtil;

import java.net.URLEncoder;

/**
 * 登录
 * Created by lqy on 2017/6/24.
 */

public class LoginActivity extends Activity {
    private static String TAG = "LoginActivity";
    private Button login;
    private String userName, passWord;
    private EditText login_username, login_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
    }

    private void initView() {
        login_username = (EditText) findViewById(R.id.login_username_text);
        login_pwd = (EditText) findViewById(R.id.login_pwd_text);
        login = (Button) findViewById(R.id.login_button);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userName = login_username.getText().toString().trim();
                passWord = login_pwd.getText().toString().trim();
                if (MyTextUtils.isEmpty(userName)) {
                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_LONG).show();
                } else if (MyTextUtils.isEmpty(passWord)) {
                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_LONG).show();
                }
                getData(userName, passWord);
            }
        });
    }

    private void getData(String username, String password) {
        String name = URLEncoder.encode(username);
        String url = Apis.api_login + "?userName=" + name + "&pass=" + password;
        DataFetch dataFetch = new DataFetch<LoginItem>(this, url, false, LoginItem.class) {
            @Override
            protected void onDataLoaded(final LoginItem data) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (data.getState() == 1) {
                            Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            saveMessage(data.getTable().get(0));
                            finish();
                        } else if (data.getState() == 0) {
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

            @Override
            protected void onDataLoadError() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "网络错误，请重新加载！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        dataFetch.LoadingData(false);
    }

    private void saveMessage(LoginBean result) {
        new SharePreferenceUtil(LoginActivity.this).setJsrCode(result.getJsr_Code());
        new SharePreferenceUtil(LoginActivity.this).setName(result.getJsr_Name());
        new SharePreferenceUtil(LoginActivity.this).setDepartment(result.getWxDw_Name());
    }
}
